﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using ESRI.ArcGIS.Controls;
using PlanningGIS.Util;
using System.Drawing;

namespace PlanningGIS.Engine.Controls
{
    /// <summary>
    /// 工具条基类
    /// </summary>
    public partial class ToolBar : Bar
    {        
        /// <summary>
        /// 构造函数
        /// </summary>
        public ToolBar()
        {
            this.SetStyle(ControlStyles.DoubleBuffer, true);
            InitializeComponent();
        }

        /// <summary>
        /// 绘制完成事件
        /// </summary>
        public event DrawFinishedEventHandler DrawFinished;        

        /// <summary>
        /// 获取或设置当前选择的绘制工具对象
        /// </summary>
        public ESRI.ArcGIS.SystemUI.ICommand CurrentCommand { get; set; }

        /// <summary>
        /// 
        /// </summary>
        private object _Hook = null;

        /// <summary>
        /// 地图管理对象，MapControl.Object
        /// </summary>
        public object Hook
        {
            get { return _Hook; }
            set
            {
                _Hook = value;
                this.timer.Enabled = true;
                CreateCmd();
            }
        }

        /// <summary>
        /// 创建按钮
        /// </summary>
        private void CreateCmd()
        {
            foreach (BaseItem btn in this.Items)
            {
                if (btn is ButtonItem && btn.Tag != null)
                {
                    if (btn.Tag is ESRI.ArcGIS.SystemUI.ICommand)
                        (btn.Tag as ESRI.ArcGIS.SystemUI.ICommand).OnCreate(Hook);
                }
            }
        }

        /// <summary>
        /// 定时检查按钮的可用状态
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void timer_Tick(object sender, EventArgs e)
        {
            if (this.Items.Count == 0) return;
            SetEnable();
        }

        /// <summary>
        /// 设置按钮的可用状态
        /// </summary>
        protected void SetEnable()
        {
            foreach (BaseItem btn in this.Items)
            {
                if (btn is ButtonItem && btn.Tag != null)
                {
                    if (btn.Tag is ESRI.ArcGIS.SystemUI.ICommand)
                        (btn as ButtonItem).Enabled = (btn.Tag as ESRI.ArcGIS.SystemUI.ICommand).Enabled;
                }
            }
        }

        /// <summary>
        /// 添加按钮项
        /// </summary>
        /// <param name="items"></param>
        /// <param name="dllname"></param>
        protected void AddItems(List<ValueName> items, string dllname = "esriControls")
        {
            if (items == null || items.Count == 0) return;
            foreach (ValueName v in items)
            {
                ButtonItem btn = new ButtonItem(v.Value, v.Name);
                Bitmap bmp = null;
                if (dllname.Equals("esriControls", StringComparison.CurrentCultureIgnoreCase))
                {
                    ESRI.ArcGIS.SystemUI.ICommand cmd = Activator.CreateInstance(Type.GetTypeFromProgID("esriControls." + v.Value)) as ESRI.ArcGIS.SystemUI.ICommand;
                    btn.Tag = cmd;
                    bmp = System.Drawing.Bitmap.FromHbitmap((IntPtr)cmd.Bitmap);
                    bmp.MakeTransparent(bmp.GetPixel(0, 0));
                    //bmp.MakeTransparent();
                }
                else
                {
                    btn.Tag = Activator.CreateInstance(Type.GetType(dllname + "." + btn.Name + "," + dllname));
                    string imagename = v.Value.Replace("ToolDraw", "New");
                    bmp = (Bitmap)(Properties.Resources.ResourceManager.GetObject(imagename));
                    bmp.MakeTransparent();
                }                
                btn.Image = bmp;
                btn.Tooltip = v.Name;
                btn.Click += new EventHandler(ButtonClick);
                this.Items.Add(btn);
            }
            
        }

        /// <summary>
        /// 按钮项点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ButtonClick(object sender, EventArgs e)
        {
            ButtonItem btn = (ButtonItem)sender;
            this.Tag = btn.Tag;
            if (btn.Tag == null) return;
            CurrentCommand = btn.Tag as ESRI.ArcGIS.SystemUI.ICommand;
            if (CurrentCommand is ESRI.ArcGIS.SystemUI.ITool)
            {
                ClearChecked();
                btn.Checked = true;
            }
            ItemClicked();
        }

        /// <summary>
        /// 清空之前的选择状态
        /// </summary>
        protected void ClearChecked()
        {
            foreach (BaseItem btn in this.Items)
            {
                if (btn is ButtonItem)
                    (btn as ButtonItem).Checked = false;
            }
        }
        
        /// <summary>
        /// 工具条项点击事件
        /// </summary>
        protected virtual void ItemClicked()
        {
            if (Hook != null && CurrentCommand != null)
            {
                //CurrentCommand.OnCreate(Hook);
                CurrentCommand.OnClick();
                if (CurrentCommand is ESRI.ArcGIS.SystemUI.ITool)
                {
                    if (Hook is IPageLayoutControl3)
                        (Hook as IPageLayoutControl3).CurrentTool = CurrentCommand as ESRI.ArcGIS.SystemUI.ITool;
                    else if (Hook is IMapControl4)
                        (Hook as IMapControl4).CurrentTool = CurrentCommand as ESRI.ArcGIS.SystemUI.ITool;
                }
            }
        }

        
    }
}
